package com.zhangck.bos2_0.Service.system.Impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhangck.bos2_0.Service.system.RoleServices;
import com.zhangck.bos2_0.dao.system.MenutRepository;
import com.zhangck.bos2_0.dao.system.PermissionRepository;
import com.zhangck.bos2_0.dao.system.RoleRepository;

import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.Permission;
import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.domain.system.User;

/** 
* @author 上午茶
* @version 创建时间：2018年6月16日 下午7:09:55 
* 类说明 
*/
@Service
@Transactional
public class RoleServicesImpl implements RoleServices{

	@Autowired
	private RoleRepository roleRepository;
	
	@Autowired
	private PermissionRepository permissionRepository;
	
	@Autowired
	private MenutRepository menutRepository;

	@Override
	public List<Role> findByUsername(User user) {
		return roleRepository.findByUsername(user.getId());
	}

	@Override
	public List<Role> findAll() {
		return roleRepository.findAll();
	}

	@Override
	public void save(Role role, String[] permissionIds, String menuIds) {
		// 保存角色信息
				roleRepository.save(role);
				// 关联权限
				if (permissionIds != null) {
					for (String permissionId : permissionIds) {
						Permission permission = permissionRepository.findOne(Integer
								.parseInt(permissionId));
						role.getPermissions().add(permission);
					}
				}
				// 关联菜单
				if (StringUtils.isNoneBlank(menuIds)) {
					String[] menuIdArray = menuIds.split(",");
					for (String menuId : menuIdArray) {
						Menu menu = menutRepository.findOne(Integer.parseInt(menuId));
						role.getMenus().add(menu);
					}
				}
	}

	

}
